public class LC_59 {
    public int[][] generateMatrix(int n) {
        int[][] arr = new int[n][n];
        int startX = 0, startY = 0, count = 1, circle = 1;
        int mid = n/2;
        while(circle <= mid){
            int i = startX, j = startY;
            for(; j < n - circle; j++){
                arr[i][j] = count++;
            }
            for(; i < n - circle; i++){
                arr[i][j] = count++;
            }
            for(; j > startY; j--){
                arr[i][j] = count++;
            }
            for(; i > startX; i--){
                arr[i][j] = count++;
            }
            startX++;
            startY++;
            circle++;
        }
        if(n % 2 == 1){
            arr[mid][mid] = count;
        }
        return arr;
    }
}
